#!/bin/sh

if [ $# -lt 5 ] ; then
    echo "Please input order by: ip port user passwd date"
    exit 0
fi

ip=$1
port=$2
user=$3
passwd=$4
input_date=$5
echo ${input_date}

zoneid=`mysql -h ${ip} -P${port} -u${user} -p${passwd} -N -e "select ZoneID from dbMagicWorld.TZoneData"`
echo ${zoneid}

mysql -h ${ip} -P ${port} -u${user} -p${passwd} -e "set @input_date:='${input_date}'; set @input_zoneid:= '${zoneid}'; source `dirname $0`/sql/DailyData.sql;"

mysql -h ${ip} -P ${port} -u${user} -p${passwd} -e "set @input_date:='${input_date}'; set @input_zoneid:= '${zoneid}'; source `dirname $0`/sql/PayDistri.sql;"


function pull_newer_data(){
        t1=`date -d "$1" +%s`  
        t2=`date -d "$2" +%s`  

        if [ $t1 -ge $t2 ]; then
        	mysql -h ${ip} -P ${port} -u${user} -p${passwd} -e "set @input_date:='${1}'; set @input_zoneid:= '${zoneid}'; set @dst_day:='${2}';  \
                                 source `dirname $0`/sql/NewerDailyData.sql;"
        fi
}

#获取开服的前三天
day1=`mysql -h ${ip} -P${port} -u${user} -p${passwd} -N -e "select min(UpdateDate) from dbMagicWorld.TOssZoneData;"`
day2=`date --date="${day1} 1 day" +%Y-%m-%d`
day3=`date --date="${day1} 2 day" +%Y-%m-%d`
pull_newer_data $input_date $day1
pull_newer_data $input_date $day2
pull_newer_data $input_date $day3
